//
//  HLComposePictureController.m
//  weibo-OC
//
//  Created by 黄力 on 15/12/26.
//  Copyright © 2015年 HLTest. All rights reserved.
//

#import "HLComposePictureController.h"
#import "HLComposePictureCell.h"

//每行cell个数
#define ComposePictureLineCount 3
//总个数
#define ComposePictureCount 1
//cell间间距
#define ComposePictureCellMargin 5

@interface HLComposePictureController ()<HLComposePictureCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>


@end

@implementation HLComposePictureController

static NSString * const reuseIdentifier = @"composePictureCell";

- (instancetype)init
{
    self = [super init];
    if (self) {
        //创建流式布局对象
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        //计算每个cell大小
        CGFloat itemW = (MainScreenWidth - (ComposePictureLineCount + 1) * ComposePictureCellMargin) / ComposePictureLineCount;
        //设置约束
        flowLayout.itemSize = CGSizeMake(itemW, itemW);
        //设置间距
        flowLayout.minimumInteritemSpacing = ComposePictureCellMargin;
        flowLayout.minimumLineSpacing = ComposePictureCellMargin;
        
        self = [super initWithCollectionViewLayout:flowLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        //设置内边距
        self.collectionView.contentInset = UIEdgeInsetsMake(ComposePictureCellMargin, ComposePictureCellMargin, 0, ComposePictureCellMargin);
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Register cell classes
    [self.collectionView registerClass:[HLComposePictureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //根据有没有到最多张数判断数量
    NSInteger count = self.composeImageArr.count == ComposePictureCount ? 0 : 1;
    return self.composeImageArr.count + count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HLComposePictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    //设置代理
    cell.delegate = self;
    //是最后一张图片时
    if (indexPath.item == self.composeImageArr.count) {
        cell.composeImage = nil;
    }else {
        cell.composeImage = self.composeImageArr[indexPath.item];
    }
    
    return cell;
}

#pragma mark - cell的代理方法
//点击添加按钮
- (void)composePictureCellDidClickAddBtn:(HLComposePictureCell *)composePictureCell {
    //创建图片选择器
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];
    //设置图片选择器的代理
    imagePickerVC.delegate = self;
    //model到图片选择器
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}

//点击删除按钮
- (void)composePictureCellDidClickDeleteBtn:(HLComposePictureCell *)composePictureCell {
    //获取选中的cell的索引
    NSIndexPath *indexPath = [self.collectionView indexPathForCell:composePictureCell];
    //删除指定的数据
    [self.composeImageArr removeObjectAtIndex:indexPath.item];
    //刷新
    [self.collectionView reloadData];
}

#pragma mark - 图片选择器代理方法
//选择图片时调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary<NSString *,id> *)editingInfo {
    //获取图片
    [self.composeImageArr addObject:image];
    //实现该方法后要手动调用dissmiss方法
    [picker dismissViewControllerAnimated:YES completion:nil];
    //刷新
    [self.collectionView reloadData];
}


#pragma mark - 懒加载
//图片数组
- (NSMutableArray *)composeImageArr {
    if (_composeImageArr == nil) {
        _composeImageArr = [NSMutableArray array];
    }
    return _composeImageArr;
}

@end
